<?php

namespace app\controller;

use app\BaseController;
use think\facade\Db;
use think\facade\Session;
use think\Request;
use app\middleware\Check;

/**
 * Class School 管理端 管理员控制器
 * @package app\controller
 */
class Admin extends BaseController
{
    protected $middleware = [Check::class];

    /** 管理员列表
     * @param Request $request
     */
    public function admin_list(Request $request)
    {
        if (!request()->isPost()) return jsonFail('请采用正确的方式登录!');
        $type = $request->param('type');//类型
        $limit = $this->request->param('limit');
        $page = $this->request->param('page');
        $token = $request->param('token');
        $name = $request->param('name','');
        //获取登录管理员身份
        $admin =Admin_find($token);
        $data = Db::table('eb_admin')
            ->alias('a')
            ->leftJoin('eb_school w','a.school_id = w.id')
            ->field('a.id,a.account,a.phone,a.status,FROM_UNIXTIME(a.create_time, "%Y/%m/%d %H:%i:%s") AS add_time,w.name')
            ->order('a.create_time', 'desc')
            ->where('a.type',2)
            ->whereLike('shop_name', '%' . $name . '%')
            ->page($page, $limit)->select()->toArray();
        $count = Db::table('eb_admin')
            ->alias('a')
            ->leftJoin('eb_school w','a.school_id = w.id')
            ->whereLike('shop_name', '%' . $name . '%')
            ->where('a.type',2)
            ->count();
        return jsonSuccess('获取成功！', ['page' => $page, 'count' => $count, 'data' => $data]);
    }

    /** 添加管理员
     * @param Request $request
     */
    public function increase(Request $request)
    {
        if (!request()->isPost()) return jsonFail('请采用正确的方式登录!');
        $token = $request->param('token');
        //获取登录管理员身份
        $admin =Admin_find($token);
        $account = $request->param('account');//用户名
        $pwd = $request->param('pwd');//qq
        $repwd = $request->param('repwd');//repwd
        $school_id = $request->param('school_id',0);//学校ID
        $phone = $request->param('phone');//手机号码
        $type = $request->param('type');//管理员类型
        $pid = $request->param('pid',0);//运营方需要平台方ID
        if($admin['type']>$type) return jsonFail('暂未权限操作，请联系系统管理员');
        if (empty($account)||empty($pwd)||empty($repwd)||empty($school_id)||empty($phone)||empty($type)) return jsonFail('缺少参数');
        if($type==3){
            if(empty($pid))return jsonFail('平台方ID不能为空');
        }
        if($pwd!==$repwd){
            return jsonFail('两次密码不一致');
        }
        //判断是否有管理员
        $account_admin = Db::table('eb_admin')->where('account',$account)->find();
        if(!empty($account_admin)) return jsonFail('用户名重复');
        try {
            $res = Db::table('eb_admin')->insertGetId([
                'account' => $account,
                'type'=>$type,
                'pid'=>$pid,
                'pwd' => sha1($pwd),
                'school_id' => $school_id,
                'phone' => $phone,
                'create_time' => time(),
            ]);
            if ($res) return jsonSuccess('添加成功！');
        } catch (\Exception $e) {
            return jsonFail('服务器内部错误');
        }
    }

    /**删除管理员
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     */
    public function delete_admin(Request $request)
    {
        if (!request()->isPost()) return jsonFail('请采用正确的方式登录!');
        $token = $request->param('token');
        //获取登录管理员身份
        $admin =Admin_find($token);
        if($admin['type']!=1) return jsonFail('暂未权限操作，请联系系统管理员');
        $id = $request->param('id');//资方ID
        try {
            $res = Db::table('eb_admin')->where('id', $id)->delete();
            if ($res) return jsonSuccess('删除成功！');
            return jsonFail('删除失败');
        } catch (\Exception $e) {
            return jsonFail('服务器内部错误');
        }
    }

    /**根据ID获取管理员信息
     * @param Request $request
     */
    public function admin_find(Request $request)
    {
        if (!request()->isPost()) return jsonFail('请采用正确的方式登录!');
        $id = $request->param('id');//资方ID
        $data = Db::table('eb_admin')->where('id',$id)->field('id,account,school_id,status,phone')->find();
        return jsonSuccess('获取成功！', $data);
    }

    /**编辑保存管理员
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     */
    public function admin_edit(Request $request)
    {
        if (!request()->isPost()) return jsonFail('请采用正确的方式登录!');
        $token = $request->param('token');
        //获取登录管理员身份
        $admin =Admin_find($token);
        $id = $request->param('id');//id
        $account = $request->param('account');//用户名
        $status = $request->param('status');//状态
        $school_id = $request->param('school_id');//学校ID
        $phone = $request->param('phone');//返现值
        $type = $request->param('type');//管理员类型
        $pid = $request->param('pid',0);//运营方需要平台方ID
        if($admin['type']>$type) return jsonFail('暂未权限操作，请联系系统管理员');
        if($type==3){
            if(empty($pid))return jsonFail('平台方ID不能为空');
        }
        try {
            $res = Db::table('eb_admin')->where('id',$id)->update([
                'account' => $account,
                'type'=>$type,
                'pid'=>$pid,
                'status'=>$status,
                'school_id' => $school_id,
                'phone' => $phone,
                'create_time' => time(),
            ]);
            if($res==0)return jsonFail('数据没有任何变化，不做更改！');
            if ($res) return jsonSuccess('编辑成功！');
        } catch (\Exception $e) {
            return jsonFail('服务器内部错误');
        }

    }

    /**修改密码
     * @param Request $request
     */
    public function change_pwd(Request $request){
        if (!request()->isPost()) return jsonFail('请采用正确的方式登录!');
        $token = $request->param('token');
        //获取登录管理员身份
        $admin =Admin_find($token);
        if($admin['type']!=1) return jsonFail('暂未权限操作，请联系系统管理员');
        $id = $request->param('id');//id
        $pwd = $request->param('pwd');//qq
        $chkpwd = $request->param('chkpwd');//chkpwd
        if($pwd!==$chkpwd){
            return jsonFail('两次密码不一致');
        }
        try {
            $res = Db::table('eb_admin')->where('id',$id)->update(['pwd'=>sha1($pwd)]);
            if ($res) return jsonSuccess('修改成功！');
        } catch (\Exception $e) {
            return jsonFail('服务器内部错误');
        }
    }


}
